<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>path.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_string_path.js.html">path.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities for dealing with POSIX path strings. Based on
<a name="line17"></a> * Python&#39;s os.path and posixpath.
<a name="line18"></a> * @author nnaze@google.com (Nathan Naze)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.string.path&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.string&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>/**
<a name="line28"></a> * Returns the final component of a pathname.
<a name="line29"></a> * See http://docs.python.org/library/os.path.html#os.path.basename
<a name="line30"></a> * @param {string} path A pathname.
<a name="line31"></a> * @return {string} path The final component of a pathname, i.e. everything
<a name="line32"></a> *     after the final slash.
<a name="line33"></a> */
<a name="line34"></a>goog.string.path.basename = function(path) {
<a name="line35"></a>  var i = path.lastIndexOf(&#39;/&#39;) + 1;
<a name="line36"></a>  return path.slice(i);
<a name="line37"></a>};
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>/**
<a name="line41"></a> * Returns the directory component of a pathname.
<a name="line42"></a> * See http://docs.python.org/library/os.path.html#os.path.dirname
<a name="line43"></a> * @param {string} path A pathname.
<a name="line44"></a> * @return {string} The directory component of a pathname, i.e. everything
<a name="line45"></a> *     leading up to the final slash.
<a name="line46"></a> */
<a name="line47"></a>goog.string.path.dirname = function(path) {
<a name="line48"></a>  var i = path.lastIndexOf(&#39;/&#39;) + 1;
<a name="line49"></a>  var head = path.slice(0, i);
<a name="line50"></a>  // If the path isn&#39;t all forward slashes, trim the trailing slashes.
<a name="line51"></a>  if (!/^\/+$/.test(head)) {
<a name="line52"></a>    head = head.replace(/\/+$/, &#39;&#39;);
<a name="line53"></a>  }
<a name="line54"></a>  return head;
<a name="line55"></a>};
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * Extracts the extension part of a pathname.
<a name="line60"></a> * @param {string} path The path name to process.
<a name="line61"></a> * @return {string} The extension if any, otherwise the empty string.
<a name="line62"></a> */
<a name="line63"></a>goog.string.path.extension = function(path) {
<a name="line64"></a>  var separator = &#39;.&#39;;
<a name="line65"></a>  // Combining all adjacent periods in the basename to a single period.
<a name="line66"></a>  var basename = goog.string.path.basename(path).replace(/\.+/g, separator);
<a name="line67"></a>  var separatorIndex = basename.lastIndexOf(separator);
<a name="line68"></a>  return separatorIndex &lt;= 0 ? &#39;&#39; : basename.substr(separatorIndex + 1);
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Joins one or more path components (e.g. &#39;foo/&#39; and &#39;bar&#39; make &#39;foo/bar&#39;).
<a name="line74"></a> * An absolute component will discard all previous component.
<a name="line75"></a> * See http://docs.python.org/library/os.path.html#os.path.join
<a name="line76"></a> * @param {...string} var_args One of more path components.
<a name="line77"></a> * @return {string} The path components joined.
<a name="line78"></a> */
<a name="line79"></a>goog.string.path.join = function(var_args) {
<a name="line80"></a>  var path = arguments[0];
<a name="line81"></a>
<a name="line82"></a>  for (var i = 1; i &lt; arguments.length; i++) {
<a name="line83"></a>    var arg = arguments[i];
<a name="line84"></a>    if (goog.string.startsWith(arg, &#39;/&#39;)) {
<a name="line85"></a>      path = arg;
<a name="line86"></a>    } else if (path == &#39;&#39; || goog.string.endsWith(arg, &#39;/&#39;)) {
<a name="line87"></a>      path += arg;
<a name="line88"></a>    } else {
<a name="line89"></a>      path += &#39;/&#39; + arg;
<a name="line90"></a>    }
<a name="line91"></a>  }
<a name="line92"></a>
<a name="line93"></a>  return path;
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Normalizes a pathname by collapsing duplicate separators, parent directory
<a name="line99"></a> * references (&#39;..&#39;), and current directory references (&#39;.&#39;).
<a name="line100"></a> * See http://docs.python.org/library/os.path.html#os.path.normpath
<a name="line101"></a> * @param {string} path One or more path components.
<a name="line102"></a> * @return {string} The path after normalization.
<a name="line103"></a> */
<a name="line104"></a>goog.string.path.normalizePath = function(path) {
<a name="line105"></a>  if (path == &#39;&#39;) {
<a name="line106"></a>    return &#39;.&#39;;
<a name="line107"></a>  }
<a name="line108"></a>
<a name="line109"></a>  var initialSlashes = &#39;&#39;;
<a name="line110"></a>  // POSIX will keep two slashes, but three or more will be collapsed to one.
<a name="line111"></a>  if (goog.string.startsWith(path, &#39;/&#39;)) {
<a name="line112"></a>    initialSlashes = &#39;/&#39;;
<a name="line113"></a>    if (goog.string.startsWith(path, &#39;//&#39;) &amp;&amp;
<a name="line114"></a>        !goog.string.startsWith(path, &#39;///&#39;)) {
<a name="line115"></a>      initialSlashes = &#39;//&#39;;
<a name="line116"></a>    }
<a name="line117"></a>  }
<a name="line118"></a>
<a name="line119"></a>  var parts = path.split(&#39;/&#39;);
<a name="line120"></a>  var newParts = [];
<a name="line121"></a>
<a name="line122"></a>  for (var i = 0; i &lt; parts.length; i++) {
<a name="line123"></a>    var part = parts[i];
<a name="line124"></a>
<a name="line125"></a>    // &#39;&#39; and &#39;.&#39; don&#39;t change the directory, ignore.
<a name="line126"></a>    if (part == &#39;&#39; || part == &#39;.&#39;) {
<a name="line127"></a>      continue;
<a name="line128"></a>    }
<a name="line129"></a>
<a name="line130"></a>    // A &#39;..&#39; should pop a directory unless this is not an absolute path and
<a name="line131"></a>    // we&#39;re at the root, or we&#39;ve travelled upwards relatively in the last
<a name="line132"></a>    // iteration.
<a name="line133"></a>    if (part != &#39;..&#39; ||
<a name="line134"></a>        (!initialSlashes &amp;&amp; !newParts.length) ||
<a name="line135"></a>        goog.array.peek(newParts) == &#39;..&#39;) {
<a name="line136"></a>      newParts.push(part);
<a name="line137"></a>    } else {
<a name="line138"></a>      newParts.pop();
<a name="line139"></a>    }
<a name="line140"></a>  }
<a name="line141"></a>
<a name="line142"></a>  var returnPath = initialSlashes + newParts.join(&#39;/&#39;);
<a name="line143"></a>  return returnPath || &#39;.&#39;;
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * Splits a pathname into &quot;dirname&quot; and &quot;basename&quot; components, where &quot;basename&quot;
<a name="line149"></a> * is everything after the final slash. Either part may return an empty string.
<a name="line150"></a> * See http://docs.python.org/library/os.path.html#os.path.split
<a name="line151"></a> * @param {string} path A pathname.
<a name="line152"></a> * @return {!Array.&lt;string&gt;} An array of [dirname, basename].
<a name="line153"></a> */
<a name="line154"></a>goog.string.path.split = function(path) {
<a name="line155"></a>  var head = goog.string.path.dirname(path);
<a name="line156"></a>  var tail = goog.string.path.basename(path);
<a name="line157"></a>  return [head, tail];
<a name="line158"></a>};
<a name="line159"></a>
<a name="line160"></a>// TODO(nnaze): Implement other useful functions from os.path
</pre>


</body>
</html>
